﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickTweet.Common;
using System.Windows.Input;
using QuickTweet.Common.Commanding;
using System.Windows;
using QuickTweet.Common.Services;
using QuickTweet.Views;
using System.Diagnostics;
using QuickTweet.Models;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace QuickTweet.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        #region Properties

        public ICommand SaveCommand { get; private set; }
        public ICommand CloseCommand { get; private set; }
        public ICommand ShowOptionsCommand { get; private set; }
        public ICommand ExitCommand { get; private set; }
        public ICommand ShowAboutCommand { get; private set; }
        public ICommand GoToTwitterCommand { get; private set; }
        public ICommand ShowAccountCommand { get; private set; }
        public ICommand DeleteAccountCommand { get; private set; }
        public ICommand EditAccountCommand { get; private set; }
        public ICommand AddAccountCommand { get; private set; }

        public List<OptionItem> Options { get; private set; }

        private OptionItem selectedOption;
        public OptionItem SelectedOption
        {
            get
            {
                return selectedOption;
            }
            set
            {
                if (UpdateProperty(value, ref selectedOption))
                    OnPropertyChanged("SelectedOption");
            }
        }

        private bool canClose;
        public bool CanClose
        {
            get
            {
                return canClose;
            }
            set
            {
                if (UpdateProperty(value, ref canClose))
                    OnPropertyChanged("CanClose");
            }
        }

        public bool IsAccountSelected
        {
            get
            {
                return (currentAccount != null);
            }
        }

        private ObservableCollection<TwitterAccount> accounts;
        public ObservableCollection<TwitterAccount> Accounts
        {
            get
            {
                return accounts;
            }
            set
            {
                if (UpdateProperty(value, ref accounts))
                    OnPropertyChanged();
            }
        }

        private TwitterAccount currentAccount;
        public TwitterAccount CurrentAccount
        {
            get
            {
                return currentAccount;
            }
            set
            {
                if (UpdateProperty(value, ref currentAccount))
                {
                    OnPropertyChanged("CurrentAccount");
                    OnPropertyChanged("IsAccountSelected");
                }
            }
        }

        #endregion

        public MainWindowViewModel()
        {
            SaveCommand = new DelegateCommand(Save);

            CanClose = false;
            CloseCommand = new DelegateCommand(null, this.CreateBinding("CanClose"));

            ShowOptionsCommand = new DelegateCommand(args => Application.Current.MainWindow.Show());
            ExitCommand = new DelegateCommand(args => Application.Current.Shutdown());
            ShowAboutCommand = new DelegateCommand(args => ShowAbout(args));
            GoToTwitterCommand = new DelegateCommand(args => Process.Start(Constants.TWITTER_URL));
            ShowAccountCommand = new DelegateCommand(args => ShowAccount(args), this.CreateBinding("IsAccountSelected"));
            DeleteAccountCommand = new DelegateCommand(args => DeleteAccount(args), this.CreateBinding("IsAccountSelected"));
            EditAccountCommand = new DelegateCommand(args => EditAccount(args), this.CreateBinding("IsAccountSelected"));
            AddAccountCommand = new DelegateCommand(args => AddAccount(args));

            Options = new List<OptionItem>();
            Options.Add(new OptionItem { Text = "Settings", Image = App.Current.GetImage("Icons/Settings.png") });
            Options.Add(new OptionItem { Text = "Accounts", Image = App.Current.GetImage("Icons/Users.png") });
            SelectedOption = Options[0];

            Accounts = new ObservableCollection<TwitterAccount>(AppSettings.TwitterAccounts);
        }        

        private void Save(object parameter)
        {
            try
            {
                AppSettings.TwitterAccounts = accounts.ToList();
                AppSettings.Save();
            }
            catch (Exception ex)
            {
                
            }
        }

        private void ShowAbout(object parameter)
        {
            // Show About Window
            var view = ServiceLocator.GetView(ViewNames.AboutView);
            view.DataContext = new AboutViewModel();
            view.ShowDialog();
        }

        private void ShowAccount(object parameter)
        {
            string url = string.Format("{0}/{1}", Constants.TWITTER_URL, currentAccount.TwitterID);
            try
            {
                Process.Start(url);
            }
            catch
            { }
        }

        private void DeleteAccount(object parameter)
        {
            string message = string.Format("Are you sure you want to delete the '{0}' account?", currentAccount.Name);
            ServiceLocator.Confirm(ConfirmFunctionNames.ConfirmAccountDelete, message, result =>
            {
                if (result)
                {
                    var newIndex = Convert.ToInt32(parameter) - 1;
                    if (newIndex < 0)
                        newIndex = 0;

                    accounts.Remove(currentAccount);
                    if (accounts.Count > 0)
                    {
                        if (accounts.Count > newIndex)
                            CurrentAccount = accounts.ElementAt(newIndex);
                        else
                            CurrentAccount = accounts.LastOrDefault();
                    }
                }
            });
        }

        private void EditAccount(object parameter)
        {
            // Show Edit Account Window
            var view = ServiceLocator.GetView(ViewNames.EditAccountView);
            view.DataContext = new EditAccountViewModel(CurrentAccount);
            view.ShowDialog();           
        }

        private void AddAccount(object parameter)
        {
            
        }
    }
}
